﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KCMS.BusinessLogic.Interfaces;
using StructureMap;
using KCMS.BusinessLogic.Models;
using KCMS.Areas.Admin.Engine;
using KCMS.Areas.Admin.Models;

namespace KCMS.Areas.Admin.Controllers
{
    [Authorize]
    public class MenuItemController : Controller
    {
        //
        // GET: /Admin/MenuItem/
        private IMenuItemService menuItemService = ObjectFactory.GetInstance<IMenuItemService>();
        private IMenuService menuService = ObjectFactory.GetInstance<IMenuService>();
        private GenderListGenerator<Menu> genderGenerator = new GenderListGenerator<Menu>();
        public ActionResult Index()
        {
            var menuItems = menuItemService.getAll();
            ViewData["menu-list"] = genderGenerator.getGenderListWithEmptyOption(menuService.getAll());
            return View(menuItems);
        }
        public ActionResult GetByMenu(Guid menuId)
        {
            List<MenuItem> menuItems = new List<MenuItem>();
            if (menuId.Equals(Guid.Empty))
            {
                menuItems.AddRange((from t in menuItemService.getAll()
                                select t).ToList());
            }
            else
            {
                menuItems.AddRange((from t in menuItemService.getAll()
                                   where t.MenuId.Equals(menuId)
                                select t).ToList());
            }
            ViewData["menu-list"] = genderGenerator.getGenderList(menuService.getAll());
            return PartialView("_MenuItemList", menuItems);
        }
        [HttpGet]
        public ActionResult Create()
        {
            var menuItem = new MenuItemEditorModel();
            menuItem.MenuList = genderGenerator.getGenderList(menuService.getAll(), Guid.Empty);
            return View(menuItem);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(MenuItemEditorModel model)
        {
            var a = 0;
            menuItemService.save(model.MenuItem);
            return RedirectToAction("Index");
        }
        [HttpGet]
        public ActionResult Edit(Guid id)
        {
            var menuItem = menuItemService.getById(id);
            var model = new MenuItemEditorModel(menuItem);
            model.MenuList = genderGenerator.getGenderList(menuService.getAll(), model.MenuItem.MenuId);
            return View(model);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(MenuItemEditorModel model)
        {
            menuItemService.update(model.MenuItem);
            return RedirectToAction("Index");
        }
        [HttpGet]
        public ActionResult Delete()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Delete(Guid id)
        {
            menuItemService.delete(id);
            return RedirectToAction("Index");
        }
        [HttpGet]
        public ActionResult Copy(Guid id)
        {
            var model = menuItemService.getById(id);
            menuItemService.save(model);
            return RedirectToAction("Index");
        }

    }
}
